/**
 * -------------------------------------------------------------*
 *                     COPYRIGHT(C) 2018                        *
 *   National Audit Office of the People’s Republic Of China    *
 *                                                              *
 *                                                              *
 *  This work contains confidential business information        *
 *  and intellectual property of CNAO.                          *
 *  All rights reserved.                                        *
 * -------------------------------------------------------------*
 */
package com.sgq.ui.component;

import java.awt.Dimension;
import java.awt.Toolkit;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import it.sauronsoftware.junique.AlreadyLockedException;
import it.sauronsoftware.junique.JUnique;

/**
 * 软件启动界面
 * @author gq2010	
 *
 */
public class Splash {
	private static final Logger LOG = LoggerFactory.getLogger(Splash.class);
	protected static Shell shell;
	private ProgressBar progressBar;
	
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		shell = new Shell(SWT.NO_TRIM | SWT.TOP);
		shell.setImage(SWTResourceManager.getImage(Splash.class, "/images/logo.ico"));
		shell.setSize(600, 400);
		shell.setText("Splash");
		//窗口居中显示
		Dimension dem = Toolkit.getDefaultToolkit().getScreenSize();
		int sHeight = dem.height;
		int sWidth = dem.width;
		int fHeight = shell.getSize().y;
		int fWidth = shell.getSize().x;
		shell.setLocation((sWidth - fWidth) / 2, (sHeight - fHeight) / 2);

		//防止软件重复开启
		String id = Splash.class.getName();
		boolean start;
		try {
			JUnique.acquireLock(id, null);
			start = true;
		} catch (AlreadyLockedException e) {
			System.out.println("实例已存在"); // 日志配置文件中的变量还未赋值
			start = false;
		}
		if (!start) {
			MessageDialog.openWarning(shell, "警告", "实例已存在，请勿重复运行！");
			System.exit(0);
			return;
		}
		
		
		try {
			Splash window = new Splash();
			window.open();
		} catch (Exception e) {
			LOG.error("启动界面初始化错误：{}", e.getMessage(), e);
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		final Display display = Display.getDefault();
		createContents(display);
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents(Display display) {
		
		progressBar = new ProgressBar(shell, SWT.NONE);
		progressBar.setBounds(0, 383, 600, 17);
		progressBar.setMaximum(5);
		progressBar.setSelection(1);
		
		Label bgLabel = new Label(shell, SWT.NONE);
		bgLabel.setImage(SWTResourceManager.getImage(Splash.class, "/images/Splash.png"));
		bgLabel.setBounds(0, 0, 600, 400);
		new Thread() {
			@Override
			public void run() {
				// 完成软件启动时的初始化操作
				display.asyncExec(()-> {
					try {
						Thread.sleep(1000);
						progressBar.setSelection(2);

						Thread.sleep(1000);
						progressBar.setSelection(3);
						
						Thread.sleep(1000);
						progressBar.setSelection(4);

						Thread.sleep(1000);
						progressBar.setSelection(5);
						
						//销毁当前shell
						shell.dispose();
					} catch (InterruptedException e) {
						LOG.error("错误", e);
						Thread.currentThread().interrupt();
					}
					//启动主程序
					new MainFrame();
				});
			}
		}.start();
	}
}
